package com.bennu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bennu.common.CustomException;
import com.bennu.domain.Category;
import com.bennu.domain.Dish;
import com.bennu.domain.Setmeal;
import com.bennu.mapper.CategoryMapper;
import com.bennu.service.CategoryService;
import com.bennu.service.DishService;
import com.bennu.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service

public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /**
     * //更具id删除分类
     * @param id
     *
     * 查询当前分类是否关联了菜品，如果已经关联，抛出一个异常
     * 查询当前分类是否关联了套餐，如果已经关联，抛出一个异常
     * 正常删除分类
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        //查询当前分类是否关联了菜品，如果已经关联，抛出一个异常
        dishLambdaQueryWrapper.eq( Dish::getCategoryId,id );
        int count = (int) dishService.count( dishLambdaQueryWrapper );
        if ( count>0 ){
            //关联了菜品，不能删除,抛出异常
            throw new CustomException( "当前分类关联了菜品，不能删除" );
        }
        //查询当前分类是否关联了套餐，如果已经关联，抛出一个异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq( Setmeal::getCategoryId,id );
        int count1 = (int) setmealService.count( setmealLambdaQueryWrapper );
        if ( count1>0 ){
            //关联了套餐，不能删除,抛出异常
            throw  new CustomException( "当前分类关联了套餐，不能删除" );
        }
        //正常删除
        super.removeById( id );
    }
}
